<template>
  <el-button @click="resetDateFilter">reset date filter</el-button>
  <el-button @click="clearFilter">reset all filters</el-button>
  <el-table ref="tableRef" row-key="date" :data="tableData" style="width: 100%">
    <el-table-column
      prop="date"
      label="Date"
      sortable
      width="180"
      column-key="date"
      :filters="[
        { text: '2016-05-01', value: '2016-05-01' },
        { text: '2016-05-02', value: '2016-05-02' },
        { text: '2016-05-03', value: '2016-05-03' },
        { text: '2016-05-04', value: '2016-05-04' },
        { text: '2016-05-05', value: '2016-05-05' },
        { text: '2016-05-06', value: '2016-05-06' },
        { text: '2016-05-07', value: '2016-05-07' },
      ]"
      :filter-method="filterHandler"
    />
    <el-table-column prop="name" label="Name" width="180" />
    <el-table-column prop="address" label="Address" :formatter="formatter" />
    <el-table-column
      prop="tag"
      label="Tag"
      width="100"
      :filters="[
        { text: 'red', value: 'red' },
        { text: 'green', value: 'green' },
        { text: 'yellow', value: 'yellow' },
      ]"
      :filter-method="filterTag"
      filter-placement="bottom-end"
    >
      <template #default="scope">
        <el-tag
          :type="scope.row.tag === 'red' ? 'danger' : scope.row.tag === 'green' ? 'success' : 'warning'"
          disable-transitions
          >{{ scope.row.tag }}</el-tag
        >
      </template>
    </el-table-column>
    <!-- 新添加的分数列 -->
    <el-table-column prop="score" label="Score" sortable width="100" />
  </el-table>
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import type { TableColumnCtx, TableInstance } from 'element-plus'

interface User {
  date: string
  name: string
  address: string
  tag: string
  score: number // 添加分数属性
}

const tableRef = ref<TableInstance>()

const resetDateFilter = () => {
  tableRef.value!.clearFilter(['date'])
}
const clearFilter = () => {
  tableRef.value!.clearFilter()
}
const formatter = (row: User, column: TableColumnCtx<User>) => {
  return row.address
}
const filterTag = (value: string, row: User) => {
  return row.tag === value
}
const filterHandler = (
  value: string,
  row: User,
  column: TableColumnCtx<User>
) => {

}

const tableData: User[] = [
  { date: '2016-05-03', name: 'Tom', address: 'No. 189, Grove St, Los Angeles', tag: 'red', score: 85 },
  { date: '2016-05-02', name: 'Tom', address: 'No. 189, Grove St, Los Angeles', tag: 'green', score: 92 },
  { date: '2016-05-04', name: 'Tom', address: 'No. 189, Grove St, Los Angeles', tag: 'yellow', score: 78 },
  { date: '2016-05-01', name: 'Tom', address: 'No. 189, Grove St, Los Angeles', tag: 'red', score: 89 },
  { date: '2016-05-05', name: 'Tom', address: 'No. 189, Grove St, Los Angeles', tag: 'green', score: 95 },
  { date: '2016-05-06', name: 'Tom', address: 'No. 189, Grove St, Los Angeles', tag: 'yellow', score: 70 },
  { date: '2016-05-07', name: 'Tom', address: 'No. 189, Grove St, Los Angeles', tag: 'red', score: 88 },
  // 添加更多数据以扩展表格长度
  { date: '2016-05-08', name: 'Tom', address: 'No. 189, Grove St, Los Angeles', tag: 'green', score: 94 },
  { date: '2016-05-09', name: 'Tom', address: 'No. 189, Grove St, Los Angeles', tag: 'yellow', score: 75 },
  { date: '2016-05-10', name: 'Tom', address: 'No. 189, Grove St, Los Angeles', tag: 'red', score: 87 },
  { date: '2016-05-11', name: 'Tom', address: 'No. 189, Grove St, Los Angeles', tag: 'green', score: 93 },
  { date: '2016-05-12', name: 'Tom', address: 'No. 189, Grove St, Los Angeles', tag: 'yellow', score: 77 },
  { date: '2016-05-13', name: 'Tom', address: 'No. 189, Grove St, Los Angeles', tag: 'red', score: 86 },
  { date: '2016-05-14', name: 'Tom', address: 'No. 189, Grove St, Los Angeles', tag: 'green', score: 91 },
  { date: '2016-05-15', name: 'Tom', address: 'No. 189, Grove St, Los Angeles', tag: 'yellow', score: 79 },
]
</script>